﻿
function GetArray(id)
{
    var obj = document.getElementById(id);
    if(obj == null)
    {
        return;
    }
    var strs = obj.value.split("@@@");
    for(var i = strs.length-1;i>=0;i--)
    {
        if(strs[i] == "")
        {
            strs.remove(i);
            break;
        }
    }
    return strs;
}

function GetDepartmnetGroups()
{
    var DepartmentGroups = GetArray(hfDepartmentGroup);
    return DepartmentGroups;
}

function GetDepartments() 
{
    var Departments = GetArray(hfDepartmentGroupDepartment);
    return Departments;
}

function GetCostCenterGroups() 
{
    var CostCenterGroups = GetArray(hfDepartmentCostCenterGroup);
    return CostCenterGroups;
}

function GetCostCenters() 
{
    var CostCenters = GetArray(hfGroupCostCenter);
    return CostCenters;
}

function MenuStr(id)
{
    return document.getElementById(id).value;
}

function Menu(theform, menuid) 
{
    var MenuClass, MenuLenArr, MenuArr, SubMenuArr, MenuIdArr
    var splitchar1, splitchar2
    var arr, subarr, arrlen

    MenuArr = new Array()
    MenuLenArr = new Array()
    SubMenuArr = new Array()
    MenuIdArr = new Array()

    MenuArr[1] = MenuStr(hfDGs);
    MenuArr[2] = MenuStr(hfDGDs);
    MenuArr[3] = MenuStr(hfDCCGs);
    MenuArr[4] = MenuStr(hfCCGCCs);
    
    MenuIdArr[1] = ddlDepartmentGroup;
    MenuIdArr[2] = ddlDepartment;
    MenuIdArr[3] = ddlCostCenterGroup;
    MenuIdArr[4] = ddlCostCenter;
    MenuClass = 4 


    splitchar1 = "|||";
    splitchar2 = "###";
    for (iii = 1; iii <= MenuClass; iii++) 
    {
        arr = MenuArr[iii].split(splitchar1);
        len = arr.length;
        subarr = new Array()
        for (i = 0; i < len; i++) 
        {
            subarr[i] = arr[i].split(splitchar2);
        }
        len = subarr.length;
        SubMenuArr[iii] = subarr
        MenuLenArr[iii] = len
    }

    var self, submenu, thislen, thisarr
    self = eval("document." + theform.name + "." + MenuIdArr[menuid])
    submenu = eval("document." + theform.name + "." + MenuIdArr[menuid + 1])

    var submenuindex = submenu.value;

    thislen = MenuLenArr[menuid + 1]
    thisarr = SubMenuArr[menuid + 1]
    submenu.length = 0
    submenu.options.add(new Option("ALL", ""));
    for (i = 0; i < thislen; i++) 
    {
        if (thisarr[i][0] == self.value) 
        {
            submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
        }
    }

    if (self.value.indexOf("ALL") > 0 || self.value == "") 
    {
        var array;
        if (menuid == 1) 
        {
            array = GetDepartments();
        }
        else if (menuid == 2) 
        {
            array = GetCostCenterGroups();
        }
        else if (menuid == 3) 
        {
            array = GetCostCenters();
        }

        for (var j = 0; j < array.length; j++) 
        {
            submenu.options.add(new Option(array[j], array[j]));
        }
    }

    if (self.value.indexOf("ALL") < 0) 
    {
        var belong = false;
        for (var k = 0; k < submenu.length; k++) 
        {
            if (submenu.options[k].value == submenuindex) 
            {
                belong = true;
                break;
            }
        }
        if (belong == true) 
        {
            submenu.value = submenuindex;
        }
        else 
        {
            submenu.options[0].selected = true;
        }
    }
    else 
    {
        submenu.value = submenuindex;
    }
}